package com.zygh.policeasset.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.regex.Pattern;

public class DoubleSerialize extends JsonSerializer<String> {

    private DecimalFormat df = new DecimalFormat("0.00");

    private DecimalFormat df1 = new DecimalFormat("0.0");

    Pattern pattern = Pattern.compile("[0-9]*");

    @Override
    public void serialize(String arg0, JsonGenerator arg1, SerializerProvider arg2) throws IOException {
        if (arg0 != null && !arg0.equals("-") && pattern.matcher(arg0).matches()) {
            Double dd = Double.parseDouble(arg0);
            arg1.writeString(df.format(dd));
        } else {
            arg1.writeString(arg0);
        }
    }
}
